
/*!	brief Application is the Main class of our well ... application.	
 * 	It keep a trace off every entities and update them every fixed time delta
 * 
 * 	The Application class will also loop for every existing View 
 *  Each view will loop throug every Entity to draw them again, and again, and 
 *  again
 */
Application=function (){}

/****************************
/*		Static Properties			
/****************************/

	Application.WindowWidth;		/*!<	Store window width 	*/
	Application.WindowHeight;		/*!<	Store window height	*/

	/*!< Store a reference to the HTML5 node "Canvas", wich is the actual 
	 *   window where we draw our graphics	*/
	Application.Canvas;		

	Application.ClearColor		=	new Vec4(0.7,0.7,0.7,1.0);	/*!< Color used to clear the scene when refreshing it	*/
	Application.Clock			=	new Clock();				/*!< Clock recording time since application start		*/

	Application.LastFrame		= 1; /*!< TimeElasped since last Frame refresh	*/
	Application.FPS;
	Application.MeanFPS			= 0;
	Application.MeanFPSCount	= 0;
	Application.RealMeanFPS		= 0;
	Application.MeanFPSRefresh	= 0;
		
/****************************
/*		Static Methods				
/****************************/
		
	/*	brief Init a WebGL application from a string reference a valid HTML canvas node	*/
	Application.Init	=	function(CanvasId)
	{
		Application.InitWebGL(CanvasId);
		Mouse.Init();
	
		Application.InitWebGLFlags();
	
		BufferObject.InitBuffers();
		GLSLProgram.New("Default", new Shader("fragment"), new Shader("vertex"));
		
	}
	
	Application.InitWebGL	=	function(CanvasId)
	{
		Application.Canvas=document.getElementById(CanvasId);
		gl = Application.Canvas.getContext("experimental-webgl");
		
		Application.WindowWidth 		= Application.Canvas.width;
		Application.WindowHeight 	= Application.Canvas.height;
	}
	
	Application.ShowFPS	=	function()
	{
		node=document.createElement("div");
		node.setAttribute("id","FPS");
		
		node.appendChild(document.createTextNode(Application.RealMeanFPS));
		
		var oldNode 	= 	document.getElementById("FPS");
		var fatherNode	=	document.getElementById("FPSFrame");
		
		fatherNode.replaceChild(node,oldNode);
	}
	
	Application.InitWebGLFlags	=	function()
	{
		gl.clearColor(Application.ClearColor.x, Application.ClearColor.y, Application.ClearColor.z, Application.ClearColor.w);
		
		gl.enable(gl.DEPTH_TEST);
		gl.disable(gl.CULL_FACE);	//	Disable Cull face tell opengl to draw face in 2 sides	*/
		
		gl.viewport(0, 0, Application.WindowWidth, Application.WindowHeight);
		gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
	}
	
	/*	brief The Update function refresh all Entities in the scene	*/
	Application.Update = function()
	{
		
		if(Application.Clock.TimeElapsed() > 10000 *Application.MeanFPSRefresh)
		{
			Application.MeanFPSRefresh+=1;
			Application.MeanFPSCount=0;
			Application.MeanFPS=0;
			Application.RealMeanFPS=0;
		}
		
		Application.FPS= 1000.0/ (Application.Clock.TimeElapsed()-Application.LastFrame);
		Application.MeanFPSCount +=1;
		Application.MeanFPS +=Application.FPS;
		Application.RealMeanFPS=Application.MeanFPS/Application.MeanFPSCount;
		Application.ShowFPS();
		Application.s_LastFrame=Application.Clock.TimeElapsed();
		
		gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
		Mouse.Update();
		Clock.Update();
		
		Application.RenderFrame();
		
	}
	
	Application.RenderEntities	=	function()
	{
		for(var i=0; i< Entity.Entities.length; i++)
		{
			Entity.Entities[i].Update();
		}
	}
	
	Application.RenderFrame	=	function()
	{
		for(var i=0;i< View.Views.length; i++)
		{
			
			View.CurrentView = View.Views[i];
			View.CurrentView.Update();
			
			Application.RenderEntities();
		}
	}
